package org.dromara.assets.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.binance.api.client.BinanceApiCallback;
import com.binance.api.client.domain.account.Order;
import com.binance.api.client.domain.event.OrderTradeUpdateEvent;
import com.binance.api.client.domain.event.UserDataUpdateEvent;
import com.binance.connector.client.spot.websocket.api.model.ExecutionReport;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.dromara.assets.biz.AssetsTradeRecordServiceBiz;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BinanceWebSocketExecutionReportHandler{

    @Autowired
    private AssetsTradeRecordServiceBiz assetsTradeRecordServiceBiz;

    //@Override
    public void hand(ExecutionReport response) {
        OrderTradeUpdateEvent event = JsonUtils.parseObject(response.toJson(), OrderTradeUpdateEvent.class);
        log.info("订单更新消息：{}", event);
        Order order = new Order();
        BeanUtil.copyProperties(event, order, true);
        order.setStatus(event.getOrderStatus());
        order.setExecutedQty(event.getAccumulatedQuantity());
        order.setCummulativeQuoteQty(event.getCumulativeQuoteQty());
        order.setOrigQty(event.getOriginalQuantity());
        order.setTime(event.getOrderCreationTime());
        order.setUpdateTime(event.getOrderTradeTime());
        //判断订单状态，为终态才处理
        if (AssetsTradeRecordServiceBiz.finalState.contains(order.getStatus().toString())) {
            assetsTradeRecordServiceBiz.syncItem(order.getSymbol(), order, Long.parseLong("4"));
        }

    }
}
